<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace temmoku\lib;
use \temmoku\db;
use \temmoku\lib\cookie;
class yzm{
	public static function Get_images($length=4,$width=80,$height=40)
    {	
	    	//这个是数学运算
	    	if(C('verification_code|Verification_Code.yzm_way')==3){
	    		$Symbol=['+','-'];
	    		$algorithm=array_rand($Symbol) ;
	    		if(C('verification_code|Verification_Code.yzm_num')){
	    			$rand1=rand  ( '0' , C('verification_code|Verification_Code.yzm_num')  );
	    			$rand2=rand  ( '0' , C('verification_code|Verification_Code.yzm_num')  );
	    		}else{
	    			$rand1=rand  ( '0' , C('verification_code|Verification_Code.yzm_num')  );
	    			$rand2=rand  ( '0' , C('verification_code|Verification_Code.yzm_num')  );
	    		}
	    		$text1=$rand1.$Symbol[$algorithm].$rand2;
	    		$text=$text1.'=?';
	    		eval("\$value=$text1;");
	    	}else{
	    		if(C('verification_code|Verification_Code.yzm_way')==0){
	    			$value=$text = _rand($length,1);
	    		}elseif(C('verification_code|Verification_Code.yzm_way')==1){
	    			$value=$text = _rand($length);
	    		}elseif(C('verification_code|Verification_Code.yzm_way')==2){
	    			$value=$text = _rand($length,'5');
	    		}elseif(C('verification_code|Verification_Code.yzm_way')==4){
	    			$value=$text = _rand($length,'2',C('yzm_china'));
	    		}
	    	}
		
        $Visiting_user=cookie::get('USER_First_Visit');
       	$yzm=db::select('*')->from(jab."yzm")->where("user_uid= '$Visiting_user' AND type='0'")->row();
       	$arr=[
				'user_uid'=>$Visiting_user,
				'value'=>$value,
				'type'=>'0',
				'posttime'=>NOWTIME
			];
       	if($yzm){
       		db::update(jab."yzm")->cols($arr)->where("user_uid='$Visiting_user'")->query();
       	}else{
       		db::insert(jab."yzm")->cols($arr)->query();
       	}
        $width = ($length*9+10)>$width?$length*9+10:$width;
        if ( function_exists('imagecreatetruecolor')) {
            $images = @imagecreatetruecolor($width,$height);
        }else {
            $images = @imagecreate($width,$height);
        }
        $r = [225,255,255,223];
        $g = [225,236,237,255];
        $b = [225,236,166,125];
        $rand = rand(0,3);
        $backColor = imagecolorallocate($images, $r[$rand],$g[$rand],$b[$rand]);
        $borderColor = imagecolorallocate($images, 100, 100, 100);
        $pointColor = imagecolorallocate($images,rand(0,255),rand(0,255),rand(0,255));
        @imagefilledrectangle($images, 0, 0, $width - 1, $height - 1, $backColor);
        @imagerectangle($images, 0, 0, $width-1, $height-1, $borderColor);
        $textColor = imagecolorallocate($images,rand(0,200),rand(0,120),rand(0,120));
        for($i=0;$i<10;$i++){
            $fontcolor=imagecolorallocate($images,rand(0,255),rand(0,255),rand(0,255));
            imagearc($images,rand(-10,$width),rand(-10,$height),rand(30,300),rand(20,200),55,44,$fontcolor);
        }
        for($i=0;$i<25;$i++){
            $fontcolor=imagecolorallocate($images,rand(0,255),rand(0,255),rand(0,255));
            imagesetpixel($images,rand(0,$width),rand(0,$height),$pointColor);
        }
        $font_file  =  Temmoku_PATH.'public/global/font/'.C('gd_water_text_font') ;
        imagefttext ( $images ,  12,  0 ,  10 ,  25 ,  $textColor ,  $font_file ,  $text );
        header("Content-type: Image/png");
        $ImageFun='Imagepng';
        $ImageFun($images);
        imagedestroy($images);
    }
	
    public static function get_yzm_code($code){
    	$Visiting_user=cookie::get('USER_First_Visit');
    	$yzm=db::select('*')->from(jab."yzm")->where("user_uid= '$Visiting_user' AND type='0'")->row();
    	if(strtolower($yzm['value'])==strtolower($code) &&$yzm){
    		db::delete(jab."yzm")->where("user_uid= '$Visiting_user'")->query();
    		$a=array('code'=>'0','text'=>'验证码正确');
    	}else{
    		$a=array('code'=>'9','text'=>'验证码错误');
    	}
    	return $a;
    }
}
?>
